package com.qyhl.module_home.home;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.sdk.statisticssdk.StatisticsMainInit;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.hawk.Hawk;
import com.qyhl.module_home.R;
import com.qyhl.module_home.R2;
import com.qyhl.module_home.city.CityFragment;
import com.qyhl.module_home.home.fragment.HomeFragment;
import com.qyhl.module_home.home.fragment2.HomeFragment2;
import com.qyhl.module_home.home.fuquan.FuQuanHomeFragment;
import com.qyhl.module_home.home.nanbu.NanBuHomeFragment;
import com.qyhl.module_home.home.panzhou.PanZhouHomeFragment;
import com.qyhl.module_home.utils.AndroidBug5497Workaround;
import com.qyhl.module_home.utils.ltab.HomeTab;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.CommonDialog;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.constant.HomeStyle;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.PushParametersBean;
import com.qyhl.webtv.commonlib.entity.VersionBean;
import com.qyhl.webtv.commonlib.entity.config.AppConfigBean;
import com.qyhl.webtv.commonlib.service.ActivitiesService;
import com.qyhl.webtv.commonlib.service.BrokeService;
import com.qyhl.webtv.commonlib.service.CircleService;
import com.qyhl.webtv.commonlib.service.LiveService;
import com.qyhl.webtv.commonlib.service.NewsService;
import com.qyhl.webtv.commonlib.service.PracticeService;
import com.qyhl.webtv.commonlib.service.ShopService;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.UpdateAPK;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.inter.HomeActivityInterface;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.NoScrollViewPager;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;


/**
 * Created by PVer on 2017/10/3.
 * 首页
 */
@Route(path = ARouterPathConstant.HOME_ACTIVITY)
public class HomeActivity extends BaseActivity implements HomeContract.HomeView, BaseActivity.InputListener {
    @BindView(R2.id.view_pager)
    NoScrollViewPager viewPager;
    @BindView(R2.id.homelayout)
    LinearLayout homelayout;
    @BindView(R2.id.tabbar)
    HomeTab tabbar;
    //tab名称
    private String[] mTitles, newTitles;

    //tab选中icon
    private String[] mSeleIcons, newSeleIcons;

    //tab未选中icon
    private String[] mNormalIcons, newNormalIcons;

    private List<Fragment> fragmentList = new ArrayList<>();
    private HomePagerAdapter mAdapter;

    //间隔时间
    private HomePresenter mPresenter;
    private long ii = 0;

    private int index = 0;

    private PushParametersBean pushBean;

    /**
     * 圈子服务
     */
    @Autowired(name = ServicePathConstant.CIRCLE_SERVICE)
    public CircleService circleService;

    @Autowired(name = ServicePathConstant.NEWS_SERVICE)
    public NewsService newsService;

    @Autowired(name = ServicePathConstant.PRACTICE_SERVICE)
    public PracticeService practiceService;

    @Autowired(name = ServicePathConstant.LIVE_SERVICE)
    public LiveService liveService;

    @Autowired(name = ServicePathConstant.ACTIVITY_SERVICE)
    public ActivitiesService activitiesService;

    @Autowired(name = ServicePathConstant.USER_SERICE)
    public UserService userService;

    @Autowired(name = ServicePathConstant.BROKE_SERVICE)
    public BrokeService brokeService;

    @Autowired(name = ServicePathConstant.SHOP_SERVICE)
    public ShopService shopService;


    /**
     * 主要回调
     */
    private HomeActivityInterface mHomeActivityInterface = new HomeActivityInterface() {
        @Override
        public void removeInputListener(InputListener inputListener) {
            HomeActivity.this.removeInputListener(inputListener);
        }

        @Override
        public void setTabbarVisibility() {
            HomeActivity.this.setTabbarVisibility();
        }

        @Override
        public void addInputListener(InputListener inputListener) {
            HomeActivity.this.addInputListener(inputListener);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        BusFactory.getBus().register(this);
        ARouter.getInstance().inject(this);
    }


    @Override
    protected int getContentView() {
        return R.layout.home_activity_home;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {

    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        MPermissionUtils.requestPermissionsResult(HomeActivity.this, 1, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE}, new MPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {

            }

            @Override
            public void onPermissionDenied() {

            }
        });

        PathConfigConstant.isLoading = Hawk.get("isLoading", false);
        mPresenter = new HomePresenter(this);
        mPresenter.getConfig();
        toH5Activity();
        pushBean = (PushParametersBean) getIntent().getSerializableExtra("msg");
        pushSkip(pushBean);
    }

    /**
     * 推送页面
     *
     * @param bean
     */
    private void pushSkip(PushParametersBean bean) {
        if (bean != null && StringUtils.isNotEmpty(bean.getNewID())) {
            RouterManager.skip(bean, HomeActivity.this);
            pushBean = null;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void pushMessage(Event.pushSkip event) {
        if (event != null) {
            if (pushBean == null) {
                pushBean = event.getBean();
                pushSkip(pushBean);
            }

        }
    }


    @Override
    protected void setListener() {

    }

    /**
     * 跳转至H5置顶的Activity
     */
    private void toH5Activity() {
        if (!StringUtils.isEmpty(PathConfigConstant.H5_PID) && !StringUtils.isEmpty(PathConfigConstant.H5_PTYPE)) {
            int type = Integer.parseInt(PathConfigConstant.H5_PTYPE);
            switch (type) {
                case 1: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY, bundle);
                }
                break;
                case 2: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.PICTURE_NEWS_ACTIVITY, bundle);
                }
                break;
                case 3: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.NEWS_CATCH, bundle);
                }
                break;
                case 4: {
                    Bundle bundle = new Bundle();
                    bundle.putString("newsId", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.SMALL_VIDEO_DETAIL_ACTIVITY, bundle);
                }
                break;
                case 5: {
                    Bundle bundle = new Bundle();
                    bundle.putString("newsId", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.GOOD_LIFE_DETAIL, bundle);
                }
                break;
                case 6:

                    break;
                case 7:
//                    Router.build("room_act")
//                            .with("roomId", Integer.parseInt(PathConfigConstant.H5_PID))
//                            .go(this);
                    break;
                case 8://链接新闻
                {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    int hasShare = (int) AppConfigUtil.getInstance().getConValue(AppConfigConstant.LINK_URL_SHARE);
                    if (hasShare == 1) {
                        bundle.putBoolean("hasShare", false);
                    }
                    RouterManager.router(ARouterPathConstant.NEWS_TITLE, bundle);
                }
                break;
                case 9: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.SCOOP_DETAIL_ACTIVIT, bundle);
                }

                break;
                case 10: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.DETAIL_TOWN, bundle);
                }
                break;
                case 11: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.DETAIL_GOV, bundle);
                }
                break;
                case 12: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.DETAIL_SPECIAL, bundle);
                }
                break;
                case 13: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.LIVE_RADIO, bundle);
                }
                break;
                case 14: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.LIVE_VIDEO, bundle);
                }
                break;
                case 15: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.CIRCLE_DETAIL, bundle);
                }
                break;
                case 16: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY, bundle);
                }
                break;
                case 17: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.LUCK_DRAW, bundle);
                }
                break;
                case 18: {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.LUCK_DRAW, bundle);
                }
                break;
                case 19: {
                    Bundle bundle = new Bundle();
                    bundle.putString("actId", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.ACT_DETAILS, bundle);
                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ACTIVITY_DETAIL);
                }
                break;
                case 20: {
                    Bundle bundle = new Bundle();
                    bundle.putString("playerId", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.ACT_PLAYER_DETAIL, bundle);
                }
                break;
                case 21: {
                    RouterManager.router(ARouterPathConstant.SHARE_HOME);
                }
                break;
                case 22: {
                    Bundle bundle = new Bundle();
                    bundle.putString("itemId", PathConfigConstant.H5_PID);
                    RouterManager.router(ARouterPathConstant.COUPON_GOODS_DETAILS_ACTIVITY, bundle);
                }
                break;
                case 44: {
                    ARouter.getInstance()
                            .build(ARouterPathConstant.SHOP_SHOP_DETAIL)
                            .withInt("shopId", Integer.parseInt(PathConfigConstant.H5_PID))
                            .navigation();

                    ActionLogUtils.getInstance().onEvent(ActionConstant.SHOP_FROM_SHARE, Integer.parseInt(PathConfigConstant.H5_PID));
                }
                default:
                    break;
            }
            PathConfigConstant.H5_PID = "";
        }
    }


    public void setTabbarVisibility() {
        tabbar.setVisibility(View.GONE);
    }

    @Override
    public void initView(AppConfigBean bean) {
        homelayout.addOnLayoutChangeListener(this);
        //软键盘adjustResize模式处理
        AndroidBug5497Workaround.assistActivity(findViewById(android.R.id.content));
        addInputListener(this);

        try {
            List<AppConfigBean.tabMenus> list = bean.getTabMenus();
            list.get(0).setCheck(true);
            mTitles = new String[list.size()];
            mNormalIcons = new String[list.size()];
            mSeleIcons = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                mTitles[i] = list.get(i).getMenuName();
                mNormalIcons[i] = list.get(i).getMenuIcon();
                mSeleIcons[i] = list.get(i).getMenuIconPre();
                switch (list.get(i).getTypeId()) {
                    //首页
                    case 1:
                        boolean isHaveCircle = false;
                        for (AppConfigBean.tabMenus tabMenus : list) {
                            if (tabMenus.getTypeId() == 17) {
                                isHaveCircle = true;
                                break;
                            }
                        }

                        if (CommonUtils.getInstance().getSite() == 148 || CommonUtils.getInstance().getSite() == 143) {
                            fragmentList.add(NanBuHomeFragment.newInstance(bean.getThreeMenus(), isHaveCircle, bean.getAppTitleLogo()));
                        } else if (CommonUtils.getInstance().getSite() == 175) {
                            fragmentList.add(PanZhouHomeFragment.newInstance(bean.getThreeMenus()));
                        }else if(CommonUtils.getInstance().getSite() == 127){
                            fragmentList.add(FuQuanHomeFragment.newInstance(bean.getThreeMenus(),bean.getCenterTabMenus(),bean.getTodayRec(),bean.getAppLogo(),bean.getAppTitleLogoAndroid()));
                        } else {
                            if (TextUtils.isEmpty(bean.getHomeStyle()) || "1".equals(bean.getHomeStyle())) {
                                fragmentList.add(HomeFragment.newInstance(isHaveCircle, bean.getAppTitleLogo()));
                            } else if ("6".equals(bean.getHomeStyle())) {
                                fragmentList.add(FuQuanHomeFragment.newInstance(bean.getThreeMenus(),bean.getCenterTabMenus(),bean.getTodayRec(),bean.getAppLogo(),bean.getAppTitleLogoAndroid()));
                            } else {
                                fragmentList.add(HomeFragment2.newInstance(bean.getHomeStyle(),
                                        bean.getAppLogo(), bean.getAppTitleLogo(),
                                        bean.getThreeMenus(), isHaveCircle));
                            }
                        }

                        break;
                    //城事
                    case 3:
                        fragmentList.add(CityFragment.newInstance());
                        break;
                    //电商
                    case 4:
                        break;
                    //个人中心
                    case 5:
                        fragmentList.add(userService.getUserCenterFragment(mHomeActivityInterface));
                        break;
                    //直播列表
                    case 6:
                        fragmentList.add(liveService.getLiveListFragment(list.get(i).getMenuName()));
                        break;
                    //网页链接
                    case 7:
                        fragmentList.add(newsService.getTitleNewsFragment(list.get(i).getMenuName(), list.get(i).getUrl()));
                        break;
                    //活动
                    case 8:
                        fragmentList.add(activitiesService.getActivityTempFragment(list.get(i).getMenuName(), false, 1));
                        break;
                    //乡镇
                    case 9:
                        String countrysideId = CommonUtils.getInstance().getCountrysideId();
                        fragmentList.add(newsService.getTownListFragment(list.get(i).getMenuName()
                                , countrysideId));
                        break;
                    //政务
                    case 10:
                        String affairsId = CommonUtils.getInstance().geTaffairsId();
                        fragmentList.add(newsService.getGovernmentListFragment(list.get(i).getMenuName()
                                , affairsId));
                        break;
                    //专题
                    case 11:
                        String sectionId = CommonUtils.getInstance().getSectionId();
                        fragmentList.add(newsService.getSpecialListFragment(list.get(i).getMenuName(), sectionId, true));
                        break;
                    //图文
                    case 12:
                        fragmentList.add(liveService.getTeleTextFragment(list.get(i).getMenuName()));
                        break;
                    //内容
                    case 13:
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            fragmentList.add(brokeService.getScoopHomeFragment(list.get(i).getMenuName(), false));
                        } else {
                            fragmentList.add(new TestFragment());
                        }
                        break;
                    //传统直播混合列表
                    case 14:

                        fragmentList.add(liveService.getMixLiveListFragment(list.get(i).getMenuName(), ""));
                        break;
                    //传统直播音频列表
                    case 15:
                        fragmentList.add(liveService.getMixLiveListFragment(list.get(i).getMenuName(), "2"));
                        break;
                    //传统直播视频列表
                    case 16:
                        fragmentList.add(liveService.getMixLiveListFragment(list.get(i).getMenuName(), "1"));
                        break;
                    //圈子
                    case 17:
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            Fragment cFragment = circleService.getCircleHomeFragment(list.get(i).getMenuName(), mHomeActivityInterface);
                            fragmentList.add(cFragment);
                        } else {
                            fragmentList.add(new TestFragment());
                        }
                        break;
                    //新活动
                    case 18:
                        fragmentList.add(activitiesService.getActivityTempFragment(list.get(i).getMenuName(), false, 1));
                        break;
                    //开票有喜
                    case 19:
                        fragmentList.add(newsService.getLuckDrawHomeFragment(list.get(i).getTabId() + "", list.get(i).getMenuName(), mHomeActivityInterface));
                        break;
                    case 20:
                        fragmentList.add(new TestFragment());
                        //优惠券
//                        fragmentList.add(GoodsHomeFragment.newInstance(list.get(i).getMenuName()));
                        break;
                    //乡镇新样式
                    case 21:
                        String countryId = CommonUtils.getInstance().getCountrysideId();
                        fragmentList.add(newsService.getTown3ListFragment(list.get(i).getMenuName(), countryId));
                        break;
                    case 22:
                        fragmentList.add(brokeService.getScoopAddPictureFragment(list.get(i).getMenuName(), mHomeActivityInterface, true));
                        break;
                    case 23:

                        fragmentList.add(liveService.getNewLiveListFragment(list.get(i).getMenuName(), true));
                        break;
                    case 24:

                        fragmentList.add(newsService.getJLNewsListFragment(0));
                        break;
                    case 25:
                        fragmentList.add(newsService.getJLNewsListFragment(1));
                        break;
                    case 26:
                        fragmentList.add(newsService.getJLMultiNewsFragment());
                        break;
                    case 30:
                        fragmentList.add(liveService.getSanDuLiveFragment(list.get(i).getUrl()));
                        break;
                    case 33:
                        //文明实践
                        fragmentList.add(practiceService.getPracticeHomeFragment(list.get(i).getUrl(), list.get(i).getMenuName(), true, HomeStyle.QIXIA, false));
                        break;
                    case 36:
                        fragmentList.add(shopService.getFragment(false, list.get(i).getMenuName()));
                        break;
                    default:
                        fragmentList.add(new TestFragment());
                        break;
                }
            }


            //初始化适配器
            mAdapter = new HomePagerAdapter(getSupportFragmentManager(), fragmentList);
            viewPager.setHasScrollAnim(false);
            viewPager.setAdapter(mAdapter);
            //设置缓存页数
//            viewPager.setOffscreenPageLimit(fragmentList == null ? 0 : (fragmentList.size() - 1));
            viewPager.setOffscreenPageLimit(3);
            //绑定viewpager
            for (String mTitle : mTitles) {
                if (StringUtils.isEmpty(mTitle)) {
                    index++;
                }
            }

            newTitles = new String[list.size() - index];
            newNormalIcons = new String[list.size() - index];
            newSeleIcons = new String[list.size() - index];
            for (int i = 0, j = 0; i < mTitles.length; i++) {
                if (StringUtils.isNotEmpty(mTitles[i])) {
                    newTitles[j] = mTitles[i];
                    newNormalIcons[j] = mNormalIcons[i];
                    newSeleIcons[j] = mSeleIcons[i];
                    j++;
                }
            }
            tabbar.setSeleUrls(newSeleIcons);
            tabbar.setNormalUrls(newNormalIcons);
            tabbar.setImgPaddint(StringUtils.dip2px(this, 2));
            if (CommonUtils.getInstance().getSite() == 175 || CommonUtils.getInstance().getSite() == 222) {
                tabbar.setSeleColor(ContextCompat.getColor(this, R.color.home_tab_bottom_select));
                tabbar.setNormalColor(ContextCompat.getColor(this, R.color.home_tab_bottom_unselect));
            } else {
                tabbar.setSeleColor(HomeThemeColorUtil.getCheckColor());
                tabbar.setNormalColor(HomeThemeColorUtil.getNormalColor());
            }
            tabbar.setDataText(newTitles);
            tabbar.setTextSize(12);
            tabbar.setImgSize(StringUtils.dip2px(this, 25), StringUtils.dip2px(this, 25));
            tabbar.setupWithViewPager(viewPager);

        } catch (Exception e) {
            e.printStackTrace();
            new CommonDialog.Builder(HomeActivity.this)
                    .setCancelable(false)
                    .setCanceledOnTouchOutside(false)
                    .setTitle("警告", R.color.global_base)
                    .setMessage("界面配置信息出错,是否重启App?")
                    .setPositiveButton("重启", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //重启App
                            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                            if (i != null) {
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            }
                            startActivity(i);
                            finish();
                        }
                    }, R.color.global_base).setNegativeButton("退出", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            }, R.color.global_black_lv2).show();
        }

        //更新
        new UpdateAPK(this, false).setListener(new UpdateAPK.OnStateListener() {
            @Override
            public void onState(String msg) {
                Toasty.normal(HomeActivity.this, msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void getData(VersionBean.DataBean bean) {
                CommonUtils.getInstance().putConfigData(AppConfigConstant.VERSION_BEAN, bean);
            }
        }).checkUpdate();

    }

    /**
     * 配置信息出错
     */
    @Override
    public void setConfigError() {
        new CommonDialog.Builder(HomeActivity.this)
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .setTitle("提示", R.color.global_black_lv1)
                .setMessage("重新获取配置信息，否则将退出", R.color.global_gray_lv1)
                .setPositiveButton("重新获取", v -> {
                    //重启App
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }, R.color.global_black_lv2)
                .setNegativeButton("残忍退出", v -> finish(), R.color.global_base).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                Long before = System.currentTimeMillis();
                if (GSYVideoManager.backFromWindowFull(HomeActivity.this)) {
                    return true;
                } else if (before - ii > 800) {
                    ii = before;
                    Toasty.normal(HomeActivity.this, "再按一次退出").show();
                    return true;
                } else {
                    //四川融媒统计
                    if (CommonUtils.getInstance().getSite() == 148 || CommonUtils.getInstance().getSite() == 145) {
                        StatisticsMainInit.appStop();
                    }
                    //删除本地数据
                    if (Hawk.contains("circle_local")) {
                        Hawk.delete("circle_local");
                    }
                    CommonUtils.getInstance().putConfigData(AppConfigConstant.VERSION_BEAN, null);
                    finish();
                }
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("首页");
        MobclickAgent.onResume(this);

    }


    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("首页");
        MobclickAgent.onPause(this);

    }

    @Override
    public void showkeyboard() {

    }

    //软键盘关闭显示底部入口
    @Override
    public void hideKeyboard() {
        tabbar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        removeInputListener(this);
        BusFactory.getBus().unregister(this);
        super.onDestroy();
    }


}
